<?php
/**
 * Created by PhpStorm.
 * User: yuanjie
 * Date: 2017/5/19
 * Time: 下午3:24
 */
class ScsDrivingLicenseController extends BaseController
{
    public function update(){
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'id' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $id = Input::get('id');
            $car_no = Input::get('car_no');
            $car_type = Input::get('car_type');
            $own = Input::get('own');
            $address = Input::get('address');
            $model = Input::get('model');
            $vin = Input::get('vin');
            $engine_number = Input::get('engine_number');
            $register_time = Input::get('register_time');
            $send_time = Input::get('send_time');
            $use_nature = Input::get('use_nature');

            $response = array('code' => 0, 'msg' => 'ok');
            $sc_user=$this->verificationSCSUserToken($token,$response);
            if(is_null($sc_user)){
                break;
            }

            try{
                $license = DrivingLicense::where('id',$id)->first();
                if(is_null($license)){
                    $response = array('code' => -1, 'msg' => 'user not exist.', 'result' => array());
                }else{
                    if($car_no != '') $license->car_no = $car_no;
                    if($car_type != '') $license->car_type = $car_type;
                    if($own != '') $license->own = $own;
                    if($address != '') $license->address = $address;
                    if($model != '') $license->model = $model;
                    if($vin != '') $license->vin = $vin;
                    if($engine_number != '') $license->engine_number = $engine_number;
                    if($register_time != '') $license->register_time = $register_time;
                    if($send_time != '') $license->send_time = $send_time;
                    if($use_nature != '') $license->use_nature = $use_nature;

                    $license->save();
                    $result = array('license' => $license->id);
                    $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
                }


            } catch (Exception $e) {
                Log::error("error", array("system" => "scs" ,"model" => "license", "event" => "update", 'input' =>Input::all() ,"exception" => $e));
                return false;
            }
        } while (false);
        Log::info("info", array("system" => "scs", "model" => "license", "event" => "update", 'input' => Input::all(), "output" => $response));
        return json_encode($response);
    }

    public function get()
    {
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
                'car_no' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $car_no = Input::get("car_no");

            //传入参数
            $response = array('code' => 0, 'msg' => 'ok');
            $sc_user = $this->verificationSCSUserToken($token, $response);
            if (is_null($sc_user)) {
                break;
            }

            $data = DrivingLicense::OrderBy('created_at','desc')
                ->select('*','id as license_id')
                ->where('car_no','like','%'.$car_no.'%')->first();

            $result = array(
                'car_no' => $car_no,
                'license' => $data
            );
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $result);
        } while (false);
        Log::info("info", array("system" => "scs", "model" => "license", "event" => "get", 'input' => Input::all(), "output" => $response));
        return json_encode($response);
    }
    
    public function createDrivingLicenseNew()
    {
        $validator = Validator::make(Input::all(),
            array(
                'token' => 'required',
            )
        );
        $response = array('code' => 0, 'msg' => 'ok', 'result' => array());
        do {
            if ($validator->fails()) {
                $response = Lang::get("scs_error.login_token_error_21005");
                break;
            }

            //传入参数
            $token = Input::get('token');
            $response = array('code' => 0, 'msg' => 'ok');
            $sc_user = $this->verificationSCSUserToken($token, $response);
            if (is_null($sc_user)) {
                break;
            }
            $dln=new DrivingLicenseNew();
            $dln->shop_id = $sc_user->shop_id;
            $dln->created_by=$sc_user->id;
            $dln->updated_by=$sc_user->id;
            if(Input::get('img')!="null")$dln->img = Input::get('img');                  
            $dln->car_no = Input::get('car_no','');
            $dln->car_type = Input::get('car_type','');
            $dln->own = Input::get('own','');
            $dln->address = Input::get('address','');
            $dln->model = Input::get('model','');
            $dln->vin = strtoupper(Input::get("vin_code",''));
            $dln->engine_number = Input::get('engine_number',0);
            $dln->register_time = Input::get('register_time',date("Y-m-d"));
            $dln->send_time = Input::get('send_time');
            $dln->use_nature = Input::get('use_nature','');
            $dln->save();
            
            $dls=new DrivingLicenseShop();
            $dls->license_id=$dln->id;
            $dls->shop_id=$sc_user->shop_id;
            $dls->created_by=$sc_user->id;
            $dls->save();

            $dlu=new DrivingLicenseUser();
            $dlu->license_id=$dln->id;
            $dlu->user_id=Input::get("user_id",'');
            $dlu->created_by=$sc_user->id;
            $dlu->save();
            
            $response = array('code' => 0, 'msg' => 'ok', 'result' => $dln->id);
        } while (false);
        Log::info("info", array("system" => "scs", "model" => "license", "event" => "get", 'input' => Input::all(), "output" => $response));
        return json_encode($response);
    }

}